//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    //登录状态
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          //用户登录公开信息
          wx.getUserInfo({
            success: res => {
              //赋值
              this.globalData.userInfo = res.userInfo;
              //回调
              if (this.getUserInfo) {
                this.getUserInfo(res)
              }
            }
          })
        }
      }
    })
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        env: 'test-9g1suh1p239bfb98',
        traceUser: true,
      })
    }
    //获取openid，存储到本地缓存
    var result = wx.cloud.callFunction({
        name: "login"
      })
      .then(res => {
        var openid = res.result.openid;
        wx.setStorageSync('openid', openid)
      })
  },
  globalData: {
    userInfo: null
  }
})